package com.dr.dac.knowledgebase;

import android.app.Application;
import android.content.Intent;

import com.dr.dac.knowledgebase.db.KClassDataBase;
import com.dr.dac.knowledgebase.db.KContentDataBase;
import com.dr.dac.knowledgebase.db.KFunctionDataBase;
import com.dr.dac.knowledgebase.ui.bean.KClass;
import com.dr.dac.knowledgebase.ui.bean.KContent;
import com.dr.dac.knowledgebase.ui.bean.KFunction;
import com.dr.dac.knowledgebase.ui.service.AppServer;

import java.util.List;

/**
 * 全局应用程序类
 *
 * @author Nimo.Yu
 * @version 1.0
 * @created 2016.12.26
 */
public class AppContext extends Application {


    private final static String TAG = "KBApplication";

    private static AppContext instance;

    KClassDataBase kClassDataBase;
    KFunctionDataBase kFunctionDataBase;
    KContentDataBase kContentDataBase;

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
        init();
    }

    private void init() {

        /**
         * TO DO
         * initSerivce()
         * initDB()
         */
        initService();
        initDB();

    }

    /**
     * 获得当前app运行的AppContext
     *
     * @return
     */
    public static AppContext getInstance() {
        return instance;
    }


    @Override
    public void onTerminate() {
        super.onTerminate();
    }

    /**
     * 初始化服务
     */
    private void initService(){
        Intent startIntent = new Intent(this, AppServer.class);
        startService(startIntent);
    }

    /**
     * 初始化数据库
     */
    private void initDB() {
        kClassDataBase = new KClassDataBase(this);
        kFunctionDataBase = new KFunctionDataBase(this);
        kContentDataBase = new KContentDataBase(this);
        setKClassData();
        setKFunctionData();
        setKContentData();
    }

    /**
     * 设置 K_Class 数据
     */
    private void setKClassData() {

        KClass kClass1 = new KClass("01", "01", "通用", "通用", "001", "通用技术文件一", "JS001", "技术文件一");
        KClass kClass2 = new KClass("02", "02", "通用", "通用", "002", "通用技术文件二", "JS002", "技术文件二");
        KClass kClass3 = new KClass("03", "03", "通用", "通用", "003", "通用技术文件三", "JS003", "技术文件三");
        KClass kClass4 = new KClass("04", "04", "通用", "通用", "004", "通用技术文件四", "JS004", "技术文件四");
        KClass kClass5 = new KClass("05", "05", "规程文件", "规程文件", "005", "XXX检验规程文件一", "GC001", "检验规程一");
        KClass kClass6 = new KClass("06", "06", "规程文件", "规程文件", "006", "XXX检验规程文件二", "GC002", "检验规程二");
        KClass kClass7 = new KClass("07", "07", "标准", "标准", "007", "GB-6726-2008", "BZ001", "验收标准");
        KClass kClass8 = new KClass("08", "08", "专用", "专用", "008", "无线电兼容规范一", "ZY001", "兼容规范一");
        KClass kClass9 = new KClass("09", "09", "专用", "专用", "009", "无线电兼容规范二", "ZY002", "兼容规范二");
        KClass kClass10 = new KClass("10", "10", "卡片", "卡片", "010", "雷达卡片成品安装合格判据", "KP001", "安装合格判据");
        KClass kClass11 = new KClass("11", "11", "卡片", "卡片", "011", "雷达卡片成品安装操作方法", "KP002", "安装操作方法");
        KClass kClass12 = new KClass("12", "12", "个人", "个人", "012", "学习安排", "GR001", "学习");
        KClass kClass13 = new KClass("13", "13", "个人", "个人", "013", "技能培训", "GR002", "培训");
        KClass kClass14 = new KClass("14", "14", "个人", "个人", "014", "职责考评", "GR003", "职责");

        kClassDataBase.insert(kClass1);
        kClassDataBase.insert(kClass2);
        kClassDataBase.insert(kClass3);
        kClassDataBase.insert(kClass4);
        kClassDataBase.insert(kClass5);
        kClassDataBase.insert(kClass6);
        kClassDataBase.insert(kClass7);
        kClassDataBase.insert(kClass8);
        kClassDataBase.insert(kClass9);
        kClassDataBase.insert(kClass10);
        kClassDataBase.insert(kClass11);
        kClassDataBase.insert(kClass12);
        kClassDataBase.insert(kClass13);
        kClassDataBase.insert(kClass14);


    }

    /**
     * 设置 K_Function 数据
     */
    private void setKFunctionData() {
        List<KFunction> list = kClassDataBase.queryFunction("");
        if (null != list && list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                if (i % 2 != 0)
                    kFunctionDataBase.insert(list.get(i));
            }
        }
    }

    /**
     * 设置 K_Content数据
     */
    /**
     * 设置 K_Content数据
     *
     * @paramContentID
     * @paramFPointID
     * @paramClassMID
     * @paramContentName
     * @paramAuthor
     * @paramContent
     * @paramPublishTime
     * @paramField
     * @paramSource
     */
    private void setKContentData() {

        KContent info1 = new KContent("001", "001", "02", "技术文件2.1", "XX", "气象雷达在准备状态，3个MFD显示器无画面显示，2个PFD显示器左下显示气象雷达信息栏内显示“准备”字符", "2016.02.03", "军工", "技术文档", "技术文件二");
        KContent info2 = new KContent("002", "001", "02", "技术文件2.2", "XX", "分别调节显示器的亮度调节旋钮，使显示亮度适中。俯仰检查：天线座扫描开关置于“OFF”时，", "2016.02.03", "军工", "技术文档", "技术文件二");
        KContent info3 = new KContent("003", "001", "02", "技术文件2.3", "XX", "天线无方位扫描运动。调节俯仰控制旋钮，平板天线作俯仰运动。5s～10s后，再置于测试状态，俯仰旋钮应能控制天线在上仰15°±2°至下俯15°±2°运动。", "2016.02.03", "军工", "技术文档", "技术文件二");

        KContent info4 = new KContent("004", "010", "10", "雷达卡片成品安装合格判据一", "赵-", "发射功能检查：分别发射“箔条”“箔条+红外”“红外”，查看发射情况是否与设置一致。", "2016.02.03", "军工", "合格判据", "安装合格判据");
        KContent info5 = new KContent("005", "010", "10", "雷达卡片成品安装合格判据二", "赵-", "按检查路线采取目视检查的方式检查", "2016.02.03", "军工", "合格判据", "安装合格判据");
        KContent info6 = new KContent("006", "010", "10", "雷达卡片成品安装合格判据三", "赵-", "按检查路线采取目视检查的方式检查", "2016.02.03", "军工", "合格判据", "安装合格判据");

        KContent info7 = new KContent("007", "005", "10", "XXX检验规程文件2.1", "赵-", "发射功能检查：分别发射“箔条”“箔条+红外”“红外”，查看发射情况是否与设置一致。", "2016.02.03", "军工", "合格判据", "检验规程二");
        KContent info8 = new KContent("008", "005", "10", "XXX检验规程文件2.2", "赵-", "按检查路线采取目视检查的方式检查", "2016.02.03", "军工", "合格判据", "检验规程二");
        KContent info9 = new KContent("009", "005", "10", "XXX检验规程文件2.3", "赵-", "按检查路线采取目视检查的方式检查", "2016.02.03", "军工", "合格判据", "检验规程二");


        kContentDataBase.insert(info1);
        kContentDataBase.insert(info2);
        kContentDataBase.insert(info3);
        kContentDataBase.insert(info4);
        kContentDataBase.insert(info5);
        kContentDataBase.insert(info6);
        kContentDataBase.insert(info7);
        kContentDataBase.insert(info8);
        kContentDataBase.insert(info9);
    }


}
